package cn.codenest.util.impl;

import cn.codenest.util.RbmqService;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ：Hyman
 * @date ：Created in 2021/7/6 12:10
 * @description：
 * @modified By：
 * @version: $
 */
public class RbmqServiceImpl implements RbmqService {

    private String rabbitMqHost = "127.0.0.1";
    private String rabbitMqUsername = "guest";
    private String rabbitMqPassword = "guest";
    private Integer rabbitMqPort = 5672;
    private Integer rabbitMqTimeOut = 10000;

    @Override
    public void sendDefaultDirectMsg(String msg) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(this.rabbitMqHost);
            factory.setPort(this.rabbitMqPort);
            factory.setConnectionTimeout(this.rabbitMqTimeOut);
            factory.setUsername(this.rabbitMqUsername);
            factory.setPassword(this.rabbitMqPassword);
            factory.setVirtualHost("/");
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("test-queue", true, false, false, null);
            channel.basicPublish("", "test-queue", null, msg.getBytes("UTF-8"));
        } finally {
            if (channel != null && channel.isOpen()) {
                channel.close();
            }
            if (connection != null && connection.isOpen()) {
                connection.close();
            }
        }
    }

    @Override
    public void sendDirectMsg(String msg) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(this.rabbitMqHost);
            factory.setPort(this.rabbitMqPort);
            factory.setConnectionTimeout(this.rabbitMqTimeOut);
            factory.setUsername(this.rabbitMqUsername);
            factory.setPassword(this.rabbitMqPassword);
            factory.setVirtualHost("/");
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("direct-exchange", "direct", true, false, null);
            channel.queueDeclare("test-queue", true, false, false, null);
            com.rabbitmq.client.AMQP.Queue.BindOk ok = channel.queueBind("test-queue", "direct-exchange", "test-queue");
            channel.basicPublish("direct-exchange", "test-queue", null, msg.getBytes("UTF-8"));
        } finally {
            if (channel != null && channel.isOpen()) {
                channel.close();
            }
            if (connection != null && connection.isOpen()) {
                connection.close();
            }
        }

    }

    @Override
    public void sendTopicMsg(String msg) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(this.rabbitMqHost);
            factory.setPort(this.rabbitMqPort);
            factory.setConnectionTimeout(this.rabbitMqTimeOut);
            factory.setUsername(this.rabbitMqUsername);
            factory.setPassword(this.rabbitMqPassword);
            factory.setVirtualHost("/");
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("topic-exchange", "topic", true, false, null);
            channel.queueDeclare("test-queue", true, false, false, null);
            com.rabbitmq.client.AMQP.Queue.BindOk ok = channel.queueBind("test-queue", "topic-exchange", "fruit");
            channel.basicPublish("topic-exchange", "fruit", null, msg.getBytes("UTF-8"));
        } finally {
            if (channel != null && channel.isOpen()) {
                channel.close();
            }
            if (connection != null && connection.isOpen()) {
                connection.close();
            }
        }
    }

    @Override
    public void sendFanoutMsg(String msg) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(this.rabbitMqHost);
            factory.setPort(this.rabbitMqPort);
            factory.setConnectionTimeout(this.rabbitMqTimeOut);
            factory.setUsername(this.rabbitMqUsername);
            factory.setPassword(this.rabbitMqPassword);
            factory.setVirtualHost("/");
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("fanout-exchange", "fanout", true, false, null);
            channel.queueDeclare("test-queue", true, false, false, null);
            com.rabbitmq.client.AMQP.Queue.BindOk ok = channel.queueBind("test-queue", "fanout-exchange", "");
            channel.basicPublish("fanout-exchange", "", null, msg.getBytes("UTF-8"));
        } finally {
            if (channel != null && channel.isOpen()) {
                channel.close();
            }
            if (connection != null && connection.isOpen()) {
                connection.close();
            }
        }
    }
}
